package com.lifeverse.entity.enums;

/**
 * 性能指标类型枚举
 */
public enum MetricType {
    
    // 系统性能指标
    CPU_USAGE("CPU使用率"),
    MEMORY_USAGE("内存使用率"),
    DISK_USAGE("磁盘使用率"),
    DISK_IO("磁盘IO"),
    NETWORK_IO("网络IO"),
    LOAD_AVERAGE("系统负载"),
    
    // JVM性能指标
    JVM_HEAP_USAGE("JVM堆内存使用率"),
    JVM_NON_HEAP_USAGE("JVM非堆内存使用率"),
    JVM_GC_COUNT("JVM垃圾回收次数"),
    JVM_GC_TIME("JVM垃圾回收时间"),
    JVM_THREAD_COUNT("JVM线程数"),
    JVM_CLASS_COUNT("JVM类加载数"),
    
    // 应用性能指标
    REQUEST_COUNT("请求数量"),
    REQUEST_RATE("请求速率"),
    RESPONSE_TIME("响应时间"),
    ERROR_RATE("错误率"),
    THROUGHPUT("吞吐量"),
    CONCURRENT_USERS("并发用户数"),
    
    // 数据库性能指标
    DB_CONNECTION_COUNT("数据库连接数"),
    DB_ACTIVE_CONNECTIONS("数据库活跃连接数"),
    DB_QUERY_TIME("数据库查询时间"),
    DB_SLOW_QUERIES("慢查询数量"),
    DB_DEADLOCKS("数据库死锁数"),
    DB_CACHE_HIT_RATE("数据库缓存命中率"),
    
    // 缓存性能指标
    CACHE_HIT_RATE("缓存命中率"),
    CACHE_MISS_RATE("缓存未命中率"),
    CACHE_EVICTION_COUNT("缓存驱逐次数"),
    CACHE_SIZE("缓存大小"),
    CACHE_MEMORY_USAGE("缓存内存使用量"),
    
    // 消息队列性能指标
    QUEUE_SIZE("队列大小"),
    QUEUE_CONSUMER_COUNT("队列消费者数量"),
    QUEUE_PRODUCER_COUNT("队列生产者数量"),
    MESSAGE_RATE("消息速率"),
    MESSAGE_PROCESSING_TIME("消息处理时间"),
    
    // 业务性能指标
    BUSINESS_TRANSACTION_COUNT("业务事务数量"),
    BUSINESS_TRANSACTION_TIME("业务事务时间"),
    BUSINESS_SUCCESS_RATE("业务成功率"),
    BUSINESS_ERROR_COUNT("业务错误数量"),
    
    // 生命体相关指标
    LIFE_ENTITY_COUNT("生命体数量"),
    LIFE_ENTITY_ACTIVE_COUNT("活跃生命体数量"),
    CONSCIOUSNESS_PROCESSING_TIME("意识处理时间"),
    DNA_EXPRESSION_TIME("DNA表达时间"),
    RELATIONSHIP_COUNT("关系数量"),
    DECISION_PROCESSING_TIME("决策处理时间"),
    
    // 网络性能指标
    NETWORK_LATENCY("网络延迟"),
    NETWORK_BANDWIDTH("网络带宽"),
    NETWORK_PACKET_LOSS("网络丢包率"),
    NETWORK_CONNECTION_COUNT("网络连接数"),
    
    // 安全相关指标
    SECURITY_THREAT_COUNT("安全威胁数量"),
    AUTHENTICATION_FAILURE_RATE("认证失败率"),
    SUSPICIOUS_ACTIVITY_COUNT("可疑活动数量"),
    
    // 自定义指标
    CUSTOM("自定义指标");
    
    private final String description;
    
    MetricType(String description) {
        this.description = description;
    }
    
    public String getDescription() {
        return description;
    }
    
    /**
     * 判断是否为系统级指标
     */
    public boolean isSystemMetric() {
        return this == CPU_USAGE || this == MEMORY_USAGE || this == DISK_USAGE ||
               this == DISK_IO || this == NETWORK_IO || this == LOAD_AVERAGE;
    }
    
    /**
     * 判断是否为JVM指标
     */
    public boolean isJvmMetric() {
        return this.name().startsWith("JVM_");
    }
    
    /**
     * 判断是否为应用指标
     */
    public boolean isApplicationMetric() {
        return this == REQUEST_COUNT || this == REQUEST_RATE || this == RESPONSE_TIME ||
               this == ERROR_RATE || this == THROUGHPUT || this == CONCURRENT_USERS;
    }
    
    /**
     * 判断是否为数据库指标
     */
    public boolean isDatabaseMetric() {
        return this.name().startsWith("DB_");
    }
    
    /**
     * 判断是否为缓存指标
     */
    public boolean isCacheMetric() {
        return this.name().startsWith("CACHE_");
    }
    
    /**
     * 判断是否为业务指标
     */
    public boolean isBusinessMetric() {
        return this.name().startsWith("BUSINESS_") || this.name().startsWith("LIFE_ENTITY_");
    }
    
    /**
     * 判断是否为安全指标
     */
    public boolean isSecurityMetric() {
        return this.name().startsWith("SECURITY_") || this == AUTHENTICATION_FAILURE_RATE ||
               this == SUSPICIOUS_ACTIVITY_COUNT;
    }
    
    /**
     * 获取指标的默认单位
     */
    public String getDefaultUnit() {
        switch (this) {
            case CPU_USAGE:
            case MEMORY_USAGE:
            case DISK_USAGE:
            case JVM_HEAP_USAGE:
            case JVM_NON_HEAP_USAGE:
            case CACHE_HIT_RATE:
            case CACHE_MISS_RATE:
            case ERROR_RATE:
            case BUSINESS_SUCCESS_RATE:
            case AUTHENTICATION_FAILURE_RATE:
            case NETWORK_PACKET_LOSS:
                return "%";
                
            case RESPONSE_TIME:
            case DB_QUERY_TIME:
            case MESSAGE_PROCESSING_TIME:
            case BUSINESS_TRANSACTION_TIME:
            case CONSCIOUSNESS_PROCESSING_TIME:
            case DNA_EXPRESSION_TIME:
            case DECISION_PROCESSING_TIME:
            case NETWORK_LATENCY:
                return "ms";
                
            case REQUEST_RATE:
            case MESSAGE_RATE:
                return "req/s";
                
            case THROUGHPUT:
                return "ops/s";
                
            case DISK_IO:
            case NETWORK_IO:
            case NETWORK_BANDWIDTH:
                return "MB/s";
                
            case JVM_GC_TIME:
                return "ms";
                
            case CACHE_SIZE:
            case CACHE_MEMORY_USAGE:
                return "MB";
                
            default:
                return "count";
        }
    }
    
    /**
     * 获取指标的推荐采样间隔（秒）
     */
    public int getRecommendedSampleInterval() {
        if (isSystemMetric() || isJvmMetric()) {
            return 30; // 系统和JVM指标每30秒采样
        } else if (isApplicationMetric()) {
            return 60; // 应用指标每分钟采样
        } else if (isDatabaseMetric() || isCacheMetric()) {
            return 60; // 数据库和缓存指标每分钟采样
        } else if (isBusinessMetric()) {
            return 300; // 业务指标每5分钟采样
        } else {
            return 60; // 默认每分钟采样
        }
    }
}